﻿Option Strict On
Option Explicit On 

Imports System.Data
Imports IP.Core.IPUserService

''' -----------------------------------------------------------------------------
''' Project	 : IPDataControl
''' Class	 : Core.IPDataControl.CFormDEDataSourceControl
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Quản lý Data source cho form DE
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[csung]	3/7/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CFormDEDataSourceControl


#Region "PUBLIC"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Tạo với sort conditon
    ''' </summary>
    ''' <param name="i_dt_datatable"></param>
    ''' <param name="i_dt_sort_condition"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New(ByVal i_dt_datatable As DataTable, ByVal i_str_sorting_clause As String)
        Debug.Assert(Not (i_dt_datatable Is Nothing), "Phải khởi tạo chứ DATATABLE- CSUNG")
        m_dv_source_dataview = New DataView(i_dt_datatable, "1=1", i_str_sorting_clause, DataViewRowState.CurrentRows)
        m_dv_source_dataview.GetEnumerator.Reset()
        make_hastable_of_rows()
        set_initial_index()
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Tạo 1 data control object, nhưng theo thứ tự bất kỳ của các hàng trong CSDL
    ''' </summary>
    ''' <param name="i_dt_datatable"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New(ByVal i_dt_datatable As DataTable)
        Debug.Assert(Not (i_dt_datatable Is Nothing), "Phải khởi tạo chứ DATATABLE- CSUNG")
        m_dv_source_dataview = New DataView(i_dt_datatable, "1=1", "ID", DataViewRowState.CurrentRows)
        m_dv_source_dataview.GetEnumerator.Reset()
        make_hastable_of_rows()
        set_initial_index()
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Chuyển con trỏ tới datarow tiếp theo và trả lại datarow đó
    ''' </summary>
    ''' <returns>Datarow tiếp theo. Nếu không có hàng nào thì trả lại null</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function next_row() As DataRow
        If m_dv_source_dataview.Count = 0 Then Return Nothing
        If m_i_current_index = m_arraylist_of_rows.Count - 1 Then Return Nothing 'at the last one
        m_i_current_index += 1
        Return get_curent_datarow()
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Trả lại datarow hiện tại
    ''' </summary>
    ''' <returns>Datarow hiện tại. Nếu không có hàng nào thì trả lại null </returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_curent_datarow() As DataRow
        If m_dv_source_dataview.Count = 0 Then
            Return Nothing
        Else
            Return CType(m_arraylist_of_rows(m_i_current_index), DataRow)
        End If
    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Chuyển con trỏ tới datarow TRƯỚC và trả lại datarow đó. Gọi để lấy hàng trước, khi user ấn nút previous
    ''' </summary>
    ''' <returns>Datarow TRƯỚC. Nếu không có hàng nào thì trả lại null</returns>
    ''' <example>
    ''' <code>
    ''' 
    ''' </code>
    ''' </example>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function previous_row() As DataRow
        If m_dv_source_dataview.Count = 0 Then Return Nothing
        If m_i_current_index = 0 Then Return Nothing 'at the first one
        m_i_current_index -= 1
        Return get_curent_datarow()
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Add datarow mới vào datatable. Gọi khi bạn đã cập nhật thành công hàng mới xuống DB
    ''' </summary>
    ''' <remarks>
    ''' datarow mới nằm ở cuối cùng, cursor cũng chỉ tới đây
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub add_data_row(ByVal i_us_object As US_Object)
        'add datarow to datatable
        Dim v_dr As DataRow = m_dv_source_dataview.Table.NewRow
        i_us_object.Me2DataRow(v_dr)
        m_dv_source_dataview.Table.Rows.Add(v_dr)
        m_dv_source_dataview.Table.AcceptChanges()
        'add to arraylist
        Dim v_i_key As Integer = m_dv_source_dataview.Count 'in the last position
        m_arraylist_of_rows.Add(v_dr)
        'set current index
        m_i_current_index = m_arraylist_of_rows.Count - 1
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Trả lại ID của row hiện tại
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' Nếu không có return  - 1
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/16/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_id_of_current_us_object() As Decimal
        Dim v_data_row As DataRow = get_curent_datarow()
        If v_data_row Is Nothing Then
            Return -1
        Else
            Return IP.Core.IPCommon.CIPConvert.ToDecimal(v_data_row("ID"))
        End If
    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="i_us_object"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub update_current_data_row(ByVal i_us_object As US_Object)
        'add datarow to datatable
        Dim v_dr As DataRow = get_curent_datarow()
        i_us_object.Me2DataRow(v_dr)
        m_dv_source_dataview.Table.AcceptChanges()
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Xóa hàng hiện tại ( đang hiện ra ) đi. Gọi khi User ấn nút Delete
    ''' </summary>
    '''  
    ''' <remarks>
    ''' datarow hiện tại sau khi đã xóa (nếu ở số 1, thì sang thứ 2. Còn lại trả về row trước
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/7/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub delete_current_datarow()
        If m_dv_source_dataview.Count = 0 Then Exit Sub
        'delete the datarow in the table
        CType(m_arraylist_of_rows(m_i_current_index), DataRow).Delete()
        m_dv_source_dataview.Table.AcceptChanges()
        'delete the datarow in the arraylist
        m_arraylist_of_rows.RemoveAt(m_i_current_index)
        'set current row index
        If m_i_current_index = m_arraylist_of_rows.Count Then
            m_i_current_index -= 1
        End If
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Go the first datarow
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function go_to_first_row() As DataRow
        If m_dv_source_dataview.Count = 0 Then Exit Function
        m_i_current_index = 0
        Return get_curent_datarow()
    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub set_initial_index()
        If m_dv_source_dataview.Count <= 0 Then
            m_i_current_index = -1
        Else
            m_i_current_index = 0
        End If
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Set the cursor on the item with the ID given
    ''' </summary>
    ''' <param name="i_id_of_us_object"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub set_index_at(ByVal i_id_of_us_object As Decimal)
        'phải bắt con chỏ phải chỉ đến đúng chỗ này
        'Dim v_arr_of_rows As DataRowView() = m_dv_source_dataview.FindRows("ID = " & i_id_of_us_object.ToString)
        Dim v_arr_of_rows As DataRow() = m_dv_source_dataview.Table.Select("ID = " & i_id_of_us_object.ToString)
        If v_arr_of_rows.Length = 1 Then 'nếu không có hàng nào thì thô
            Dim v_row_to_find As DataRow = v_arr_of_rows(0)
            m_i_current_index = m_arraylist_of_rows.IndexOf(v_row_to_find)
        End If
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Number of rows here
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_number_of_rows() As Integer
        Return m_arraylist_of_rows.Count
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Current row index
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_current_row_index() As Integer
        Return m_i_current_index
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_str_current_per_total() As String
        Return (get_current_row_index() + 1).ToString & " / " & get_number_of_rows.ToString
    End Function
#End Region

#Region "Members"
    Private m_dv_source_dataview As DataView
    Private m_arraylist_of_rows As ArrayList
    Private m_i_current_index As Integer = 0
#End Region

#Region "Private"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Tạo ra 1 hastable từ các row
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/8/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub make_hastable_of_rows()
        Dim v_drv As DataRowView
        Dim v_index As Integer = 0
        m_arraylist_of_rows = New ArrayList
        For Each v_drv In m_dv_source_dataview
            m_arraylist_of_rows.Add(v_drv.Row)
            v_index += 1
        Next
    End Sub
#End Region
End Class
